<template>
  <el-dialog
    :title="!dataForm.publishId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="发布标题" prop="publishTitle">
      <el-input v-model="dataForm.publishTitle" placeholder="发布标题"></el-input>
    </el-form-item>
    <el-form-item label="发布内容" prop="publishDesc">
      <el-input v-model="dataForm.publishDesc" placeholder="发布内容"></el-input>
    </el-form-item>
    <el-form-item label="发布时间" prop="publishTime">
      <el-input v-model="dataForm.publishTime" placeholder="发布时间"></el-input>
    </el-form-item>
    <el-form-item label="1-楼市圈消息发布 
2-二手房出售
 3- 有房出租
4-  我想租个房
5- 我想买个房" prop="publishType">
      <el-input v-model="dataForm.publishType" placeholder="1-楼市圈消息发布 
2-二手房出售
 3- 有房出租
4-  我想租个房
5- 我想买个房"></el-input>
    </el-form-item>
    <el-form-item label="关联id" prop="relevanceId">
      <el-input v-model="dataForm.relevanceId" placeholder="关联id"></el-input>
    </el-form-item>
    <el-form-item label="费用" prop="money">
      <el-input v-model="dataForm.money" placeholder="费用"></el-input>
    </el-form-item>
    <el-form-item label="发布人ID" prop="peopleId">
      <el-input v-model="dataForm.peopleId" placeholder="发布人ID"></el-input>
    </el-form-item>
    <el-form-item label="发布人类型 1-用户 2 -经纪人" prop="peopleType">
      <el-input v-model="dataForm.peopleType" placeholder="发布人类型 1-用户 2 -经纪人"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          publishId: 0,
          publishTitle: '',
          publishDesc: '',
          publishTime: '',
          publishType: '',
          relevanceId: '',
          money: '',
          peopleId: '',
          peopleType: ''
        },
        dataRule: {
          publishTitle: [
            { required: true, message: '发布标题不能为空', trigger: 'blur' }
          ],
          publishDesc: [
            { required: true, message: '发布内容不能为空', trigger: 'blur' }
          ],
          publishTime: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          publishType: [
            { required: true, message: '1-楼市圈消息发布 
2-二手房出售
 3- 有房出租
4-  我想租个房
5- 我想买个房不能为空', trigger: 'blur' }
          ],
          relevanceId: [
            { required: true, message: '关联id不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '费用不能为空', trigger: 'blur' }
          ],
          peopleId: [
            { required: true, message: '发布人ID不能为空', trigger: 'blur' }
          ],
          peopleType: [
            { required: true, message: '发布人类型 1-用户 2 -经纪人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.publishId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.publishId) {
            this.$http({
              url: this.$http.adornUrl(`/publish/publish/info/${this.dataForm.publishId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.publishTitle = data.publish.publishTitle
                this.dataForm.publishDesc = data.publish.publishDesc
                this.dataForm.publishTime = data.publish.publishTime
                this.dataForm.publishType = data.publish.publishType
                this.dataForm.relevanceId = data.publish.relevanceId
                this.dataForm.money = data.publish.money
                this.dataForm.peopleId = data.publish.peopleId
                this.dataForm.peopleType = data.publish.peopleType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/publish/publish/${!this.dataForm.publishId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'publishId': this.dataForm.publishId || undefined,
                'publishTitle': this.dataForm.publishTitle,
                'publishDesc': this.dataForm.publishDesc,
                'publishTime': this.dataForm.publishTime,
                'publishType': this.dataForm.publishType,
                'relevanceId': this.dataForm.relevanceId,
                'money': this.dataForm.money,
                'peopleId': this.dataForm.peopleId,
                'peopleType': this.dataForm.peopleType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
